local a = CreateFrame("Frame")

local isFriend = function(player)
	if a.db.blockfriends then
		return false
	end

	if GetNumFriends() > 0 then
		for i = 1, GetNumFriends() do
			if GetFriendInfo(i) == player then
				return true
			end
		end
	end

	if IsInGuild() then
		for i = 1, GetNumGuildMembers() do
			if GetGuildRosterInfo(i) == player then
				return true
			end
		end
	end

	return false
end

a.DUEL_REQUESTED = function(self, ...)
	if not isFriend(arg1) then
		HideUIPanel(StaticPopup1)
		CacelDuel()
	end
end

a.ADDON_LOADED = function(self, ...)
	if arg1 == "AntiSocial" then
		local _G = getfenv(0)
		local db = _G.AntiSocialDB
		local name,realm = UnitName("player"), GetRealmName()
		local data = {
			enable = true,
			blockfriend = false,
		}

		if not db then
			db = {
				[realm] = {
					[name] = data
				},
			}
			_G.AntiSocialDB = db
		end

		if not db[realm] then
			db[realm] = {
				[name] = data
			}
		end

		if not db[realm][name] then
			db[realm][name] = data
		end

		self.db = db[realm][name]
		self:UnregisterEvent("ADDON_LOADED")
		ChatFrame1:AddMessage("AntiSocial: Init complete")
	end
end

a:SetScript("OnEvent", function(self, event, ...)
	self[event](self, ...)
end)
a:RegisterEvent("DUEL_REQUESTED")
a:RegisterEvent("ADDON_LOADED")
